# coding:utf-8
from PyQt6.QtWidgets import QWidget, QFileDialog
from qfluentwidgets import BodyLabel, setFont, ExpandLayout, SettingCardGroup, FluentIcon as FIF, SwitchSettingCard, \
    OptionsSettingCard, CustomColorSettingCard, setTheme, setThemeColor, InfoBar, HyperlinkCard, PrimaryPushSettingCard, \
    PushSettingCard

from common import cfg, signalBus, HELP_URL, COPYRIGHT, FEEDBACK_URL, SingleIcon
from components import BaseWidget


class SettingInterface(BaseWidget):
    def __init__(self, parent: QWidget):
        super().__init__(parent)
        self.titleLabel = BodyLabel('设置', self)
        self.scrollWidget = QWidget(self)
        self.scrollLayout = ExpandLayout(self.scrollWidget)
        # 路径设置
        self.pathGroup = SettingCardGroup('路径设置', self.scrollWidget)
        self.ffmpegCard = PushSettingCard(
            '选择FFmpeg',
            SingleIcon.FFmpeg,
            '设置FFmpeg路径',
            cfg.get(cfg.ffmpegFolder),
            self.pathGroup
        )
        # 个性化设置
        self.personalGroup = SettingCardGroup('个性化设置', self.scrollWidget)
        self.micaCard = SwitchSettingCard(
            FIF.TRANSPARENT,
            '云母效果',
            '',
            cfg.micaEnabled,
            self.personalGroup
        )
        self.themeCard = OptionsSettingCard(
            cfg.themeMode,
            FIF.BRUSH,
            '应用主题',
            '',
            texts=['浅色', '深色', '跟随系统'],
            parent=self.personalGroup
        )
        self.themeColorCard = CustomColorSettingCard(
            cfg.themeColor,
            FIF.PALETTE,
            '主题颜色',
            '',
            self.personalGroup
        )
        self.zoomCard = OptionsSettingCard(
            cfg.dpiScale,
            FIF.ZOOM,
            '界面缩放',
            '',
            texts=["100%", "125%", "150%", "175%", "200%", "跟随系统设置"],
            parent=self.personalGroup
        )

        # 关于
        self.aboutGroup = SettingCardGroup('关于', self.scrollWidget)
        self.helpCard = HyperlinkCard(
            HELP_URL,
            '打开帮助页面',
            FIF.HELP,
            '帮助',
            '',
            self.aboutGroup
        )
        self.feedbackCard = HyperlinkCard(
            FEEDBACK_URL,
            '提供反馈',
            FIF.FEEDBACK,
            '提供反馈',
            '',
            self.aboutGroup
        )
        self.aboutCard = PrimaryPushSettingCard(
            '检查更新',
            FIF.INFO,
            '关于',
            COPYRIGHT,
            self.aboutGroup
        )

        self.__initWidgets()
        self.__initLayout()
        self.__connectSignalToSlot()

    def __initWidgets(self):
        self.setObjectName('SettingInterface')
        self.setWidget(self.scrollWidget)
        setFont(self.titleLabel, 33)

    def __initLayout(self):
        self.topLayout.addWidget(self.titleLabel)
        self.scrollLayout.setSpacing(20)
        self.scrollLayout.setContentsMargins(36, 10, 36, 10)
        self.pathGroup.addSettingCards([self.ffmpegCard])
        self.scrollLayout.addWidget(self.pathGroup)
        self.personalGroup.addSettingCards([self.micaCard, self.themeCard, self.themeColorCard, self.zoomCard])
        self.scrollLayout.addWidget(self.personalGroup)
        self.aboutGroup.addSettingCards([self.helpCard, self.feedbackCard, self.aboutCard])
        self.scrollLayout.addWidget(self.aboutGroup)

    def __connectSignalToSlot(self):
        """ 连接信号到插槽 """
        cfg.appRestartSig.connect(self.__showRestartTooltip)
        # 路径设置
        self.ffmpegCard.clicked.connect(lambda: self.__selectFFmpegFolder())
        # 个性化设置
        cfg.themeChanged.connect(setTheme)
        self.themeColorCard.colorChanged.connect(lambda c: setThemeColor(c))
        self.micaCard.checkedChanged.connect(signalBus.micaEnableChanged)
        # 关于
        self.aboutCard.clicked.connect(self.__checkUpdate)

    def __showRestartTooltip(self):
        """ 显示重启工具提示 """
        InfoBar.success(
            '更新成功',
            '重启后配置生效',
            duration=2000,
            parent=self.window()
        )

    def __selectFFmpegFolder(self):
        """ 选择 FFmpeg 路径 """
        folder, _ = QFileDialog.getOpenFileName(self, '选择 FFmpeg 路径', cfg.get(cfg.ffmpegFolder),
                                                'FFmpeg Executable (*.exe)')
        self.ffmpegCard.setContent(folder)
        cfg.set(cfg.ffmpegFolder, folder)

    def __checkUpdate(self):
        """ 检查更新 """
        # TODO: 实现更新功能
        pass
